package com.asset.dispoal.plaintiffConfiguration.service.commom;

import com.asset.dispoal.commom.page.PagingQuery;
import com.asset.dispoal.plaintiffConfiguration.domain.db.entity.SysPlaintiffConfiguration;
import com.asset.dispoal.plaintiffConfiguration.domain.db.entity.SysPlaintiffCourt;
import com.asset.dispoal.plaintiffConfiguration.domain.db.mapper.SysPlaintiffConfigurationMapper;
import com.asset.dispoal.plaintiffConfiguration.dto.CourtResponse;
import com.asset.dispoal.plaintiffConfiguration.dto.SysPlaintiffConfigurationRequest;
import com.asset.dispoal.plaintiffConfiguration.dto.SysPlaintiffConfigurationResponse;
import com.asset.dispoal.plaintiffConfiguration.service.converter.PlaintiffConfigurationConverter;
import com.asset.dispoal.plaintiffFile.dto.PlaintiffFileResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.util.CollectionUtil;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author qr
 * @date 2022/3/21$
 */
@Component
public class PlaintiffConfigurationQueryPageCommand implements BaseCommand<SysPlaintiffConfigurationRequest, Result> {

    @Autowired
    SysPlaintiffConfigurationMapper sysPlaintiffConfigurationMapper;


    @Override
    public Result<PageVO<SysPlaintiffConfigurationResponse>> execute(SysPlaintiffConfigurationRequest sysPlaintiffConfigurationRequest) {

        SysPlaintiffConfiguration sysPlaintiffConfiguration = PlaintiffConfigurationConverter.INSTANCE.reqToDo(sysPlaintiffConfigurationRequest);
        sysPlaintiffConfiguration.setPermission((String) GlobalHeaderThreadLocal.getOrException().getExtraMap().get("permission"));
        int count = sysPlaintiffConfigurationMapper.count(sysPlaintiffConfiguration);
        Page<SysPlaintiffConfiguration> page = PagingQuery.getPage(count, sysPlaintiffConfiguration);
        PageVO<SysPlaintiffConfigurationResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);
        if (page != null) {
            List<PlaintiffFileResponse> businessLicense;
            List<PlaintiffFileResponse> idCard;
            List<PlaintiffFileResponse> responseList;
            List<PlaintiffFileResponse> stampFil;
            List<SysPlaintiffConfiguration> plaintiffConfigurationList = sysPlaintiffConfigurationMapper.page(page);
            List<String> plaintiffId = plaintiffConfigurationList.stream().map(SysPlaintiffConfiguration::getId).collect(Collectors.toList());
            List<PlaintiffFileResponse> plaintiffFileResponses = sysPlaintiffConfigurationMapper.queryPlaintiffFile(plaintiffId);
            List<SysPlaintiffCourt> plaintiffCourtList = sysPlaintiffConfigurationMapper.queryPlaintiffCourtByPlaintiffIdList(plaintiffId);

            final Map<String, List<PlaintiffFileResponse>> collect = plaintiffFileResponses.stream().collect(Collectors.groupingBy(PlaintiffFileResponse::getPlaintiffId));
            for (SysPlaintiffConfiguration plaintiffConfiguration : plaintiffConfigurationList) {
                if (CollectionUtil.isNotEmpty(plaintiffCourtList)) {
                    List<CourtResponse> courtList = plaintiffCourtList.stream().filter(plaintiffCourt -> plaintiffCourt.getPlaintiffId().equals(plaintiffConfiguration.getId())).map(x -> {
                        CourtResponse courtResponse = new CourtResponse();
                        courtResponse.setCourtId(x.getCourtId());
                        courtResponse.setCourtName(x.getCourtName());
                        return courtResponse;
                    }).collect(Collectors.toList());
                    plaintiffConfiguration.setCourtList(courtList);
                }
                List<PlaintiffFileResponse> plaintiffFileResponseList = collect.get(plaintiffConfiguration.getId());
                if (CollectionUtil.isEmpty(plaintiffFileResponseList)) {
                    continue;
                }
                Map<String, List<PlaintiffFileResponse>> stringListMap = plaintiffFileResponseList.stream().collect(Collectors.groupingBy(PlaintiffFileResponse::getFileType));
                businessLicense = stringListMap.get("business_license");
                idCard = stringListMap.get("id_card");
                responseList = stringListMap.get("legal_person_identification");
                stampFil = stringListMap.get("stamp_file");
                plaintiffConfiguration.setBusinessLicenseFile(businessLicense);
                plaintiffConfiguration.setIdCardFile(idCard);
                plaintiffConfiguration.setLegalRepresentativeProveFile(responseList);
                plaintiffConfiguration.setSealFile(stampFil);

            }
            List<SysPlaintiffConfigurationResponse> sysPlaintiffConfigurationResponses = PlaintiffConfigurationConverter.INSTANCE.doToRes(plaintiffConfigurationList);
            pageVO.setResult(sysPlaintiffConfigurationResponses);
        }
        return Result.ok(pageVO);
    }
}
